(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.jobGroup.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', '$q', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'jobGroupService' ];
	function updateController($scope, Constants, $q, modalService, gridService, layoutConfig, validatorConfig, authService, jobGroupService) {
		var ctrl = this, jobGroupInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.jobGroup = {};
		ctrl.jobGroup.jobgrpDepencyList = ctrl.jobGroup.jobgrpDepencyList || [];
		ctrl.jobGroup.jobgrpSchd = ctrl.jobGroup.jobgrpSchd || {};
		ctrl.jobGroup.jobGroupPreParmetes = ctrl.jobGroup.jobGroupPreParmetes || [];
		ctrl.jobGroup.jobGroupPostParmetes = ctrl.jobGroup.jobGroupPostParmetes || [];

		ctrl.viewJobs = function(){
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.job',
				'title' : 'title.dialogs.info.jobs',
				'params' : jobGroupInfo
			});
		}
		ctrl.init = function() {
			var params = {
				jobGroupId : jobGroupInfo.jobGroupId
			};
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobgrpDepency.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.jobGroup.jobgrpDepencyList';
			
			//add sub data validator
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.jobGroupPreParameters.tableConfigs.table.validatorConfig = ctrl.validatorConfig.jobGroupPreParmetes;
			ctrl.jobPreParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobGroupPreParameters.tableConfigs.table,parPreSelectionChanged);
			ctrl.jobPreParmetesOptions.data = 'vm.jobGroup.jobGroupPreParmetes';
			//add sub data validator
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.jobGroupPostParameters.tableConfigs.table.validatorConfig = ctrl.validatorConfig.jobGroupPostParmetes;
			
			ctrl.jobPostParmetesOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobGroupPostParameters.tableConfigs.table,parPostSelectionChanged);
			ctrl.jobPostParmetesOptions.data = 'vm.jobGroup.jobGroupPostParmetes';
			
			jobGroupService.initDataOptions(ctrl);

			authService.update(params).then(function(response) {
				ctrl.jobGroup = response.data || jobGroupInfo;
				ctrl.jobGroup.jobgrpDepencyList = ctrl.jobGroup.jobgrpDepencyList || [];
				ctrl.jobGroup.jobgrpSchd = ctrl.jobGroup.jobgrpSchd || {};
				ctrl.jobGroup.jobGroupPreParmetes = ctrl.jobGroup.jobGroupPreParmetes || [];
				ctrl.jobGroup.jobGroupPostParmetes = ctrl.jobGroup.jobGroupPostParmetes || [];
			});
		};
		ctrl.testSchd = function(){
			if (!ctrl.jobGroup.jobgrpSchd.runTimes) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.group.schedule.run.times.required' ]
				});
				return false;
			}
			jobGroupService.testSchd(ctrl.jobGroup.jobgrpSchd);
		};
		ctrl.updateSave = function() {
			if(jobGroupService.checkParameter(ctrl.jobGroup.jobGroupPreParmetes)
					|| jobGroupService.checkParameter(ctrl.jobGroup.jobGroupPostParmetes)){
					modalService.openErrorDialogs({
						messages : [ 'title.dialogs.jobgroup.parameter.name.can.not.repeate' ]
					});
					return;
				}
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.jobGroup.taskId) {
				return authService.updateContinueSave(ctrl.jobGroup).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.jobGroup).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.jobGroup.taskId) {
				return authService.updateContinueSubmit(ctrl.jobGroup).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.jobGroup).then(submitSuccessful, submitFailed);
			}
		};

		ctrl.addJobGroupDepency = function(row) {
			var deferred = $q.defer();
			var promise = deferred.promise;
			if (!ctrl.jobGroup.jobGroupName) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.job.group.name.input' ]
				});
				deferred.reject();
			}else{
				modalService.openCustomDialogs({
					'dialogsModule' : 'cda.jobGroup',
					'title' : 'title.dialogs.jobgroup',
					'params' : {
						"jobgrpDepencyList" : ctrl.jobGroup.jobgrpDepencyList
					},
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								deferred.reject();
								return promise;
							}
							angular.merge(row, selectedRows[0]);
							row.depencyJobgrpId = selectedRows[0].jobGroupId;
							row.jobGroupId = ctrl.jobGroup.jobGroupId;
							row.dataStatus = "C";
							deferred.resolve(selectedRows[0]);
						},
						'close' : function(){
							deferred.reject();
						}
					}
				});
			}
			return promise;
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedJobGroupDepencyList = selectedRows;
		}
		function parPreSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedjobGroupPreParameters = selectedRows;
		}
		function parPostSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedjobGroupPostParameters = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.jobGroup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.jobgroup.list',
					'state' : 'cda.jobGroup.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.jobGroup = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);